﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TruongVuWebsite.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        private const string UserName = "Administrator";
        private const string Password = "TruongVu123!";
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies["UserInfo"];
            if (cookie != null)
            {
                string username = cookie["UserName"];
                string pass = cookie["Password"];
             
                if (username == UserName && pass == Password)
                {
                    Session["IsLogin"] = 1;
                    Session["UserName"] = username;
                }
            }
            if (Session["IsLogin"] == null || (int)Session["IsLogin"] == 0)
            {
                pnDangNhap.Visible = true;
                pnDaDangNhap.Visible = false;
            }
            else
            {
                pnDaDangNhap.Visible = true;
                pnDangNhap.Visible = false;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            if (this.txtUserName.Text == UserName && this.txtPassword.Text == Password)
            {
                Session["IsLogin"] = 1;
                Session["UserName"] = UserName;
                if (rdoremember.Checked == true)
                {
                    HttpCookie cookie = new HttpCookie("UserInfo");
                    cookie["UserName"] = txtUserName.Text;
                    cookie["Password"] = txtPassword.Text;
                    cookie.Expires = DateTime.Now.AddDays(10);
                    Response.Cookies.Add(cookie);
                }

                Response.Redirect("~/Admin/Index.aspx");
            }
            else
            {
                lbError.Text = "Tên đăng nhập hoặc mật khẩu không đúng!\nVui lòng thử lại";
            }
        }

        protected void btnDangNhapLai_Click(object sender, EventArgs e)
        {
            pnDangNhap.Visible = true;
            pnDaDangNhap.Visible = false;
        }
    }
}